package gov.pbc.nn.kjgl.gzjhgl.view;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import gov.pbc.nn.core.dao.PbcnnCoreHibernateDao;
import gov.pbc.nn.core.utils.SecurityUtils;
import gov.pbc.nn.kjgl.Constants;
import gov.pbc.nn.kjgl.entity.WorkPlan;
import gov.pbc.nn.kjgl.gzjhgl.dao.WorkPlanDao;
import gov.pbc.nn.uflo.entity.Opinion;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.bstek.bdf2.core.business.IUser;
import com.bstek.bdf2.core.context.ContextHolder;
import com.bstek.dorado.annotation.DataProvider;
import com.bstek.dorado.annotation.DataResolver;
import com.bstek.dorado.annotation.Expose;
import com.bstek.dorado.data.provider.Criteria;
import com.bstek.dorado.data.provider.Page;

/**
 * 工作计划的数据库和页面的中间项 主要kezhangPlan.view and PlanInput.view使用
 * 
 * @author jack.zhao
 * 
 */
@Component("workPlanPRR")
public class WorkPlanPR {

	// 状态需要修改
	// 0是初始化状态
	// 10：科长审核
	// 20：处长审核
	// 30：计划执行中
	// 40：是科长驳回
	// 50:是处领导驳回
	// 60 是计划完成！不管是否完成

	/**
	 * 注入后台操作的dao类
	 */
	@Autowired
	private WorkPlanDao workPlanDao;

	/**
	 * 注入操作core项目的类
	 */
	@Resource(name = PbcnnCoreHibernateDao.BEAN_ID)
	private PbcnnCoreHibernateDao coreHibernateDao;

	/**
	 * 一个静态字符串
	 */

	/**
	 * 根据当前登陆人的返回，可以操作的page
	 * 
	 * @auther jack.zhao
	 * @param page
	 * @param criteria
	 * 
	 */
	@DataProvider
	public void getAllPage(Page<WorkPlan> page, Criteria criteria) {
		workPlanDao.getPage(page, criteria);
	}

	/**
	 * 科长的getpage数据加载
	 * 
	 * @auther jack.zhao
	 * @param page
	 * @param criteria
	 * 
	 */
	@DataProvider
	public void getKezhangPage(Page<WorkPlan> page, Criteria criteria) {
		workPlanDao.getKezhangPage(page, criteria);
	}

	/**
	 * 处长的getpage数据加载
	 * 
	 * @auther jack.zhao
	 * @param page
	 * @param criteria
	 * 
	 */
	@DataProvider
	public void getChuzhangPage(Page<WorkPlan> page, Criteria criteria) {
		workPlanDao.getChuzhangPage(page, criteria);
	}

	/**
	 * 根据登录人判断返回的数据
	 * 
	 * @auther jack.zhao
	 * @param page
	 * @param criteria
	 * 
	 */
	@DataProvider
	public void getShenhePage(Page<WorkPlan> page, Criteria criteria) {
		String username = ContextHolder.getLoginUserName();
		if (SecurityUtils.containsRole(username, Constants.ROLE_KZ)) {
			workPlanDao.getKezhangPage(page, criteria);
		}
		if (page.getEntityCount() == 0) {
			if (SecurityUtils.containsRole(username, Constants.ROLE_CZ)) {
				workPlanDao.getChuzhangPage(page, criteria);
			}
		}
	}

	/**
	 * 得到当前登录，如果是科长返回kz，如果是处长返回cz，前台根据返回的字符传控制按钮的显示
	 * 
	 * @auther jack.zhao
	 * @return
	 * 
	 */
	@Expose
	public String getButtonState() {
		String username = ContextHolder.getLoginUserName();
		String state = null;
		if (SecurityUtils.containsRole(username, Constants.ROLE_KZ)) {
			state = "kz";
		}
		if (SecurityUtils.containsRole(username, Constants.ROLE_CZ)) {
			state = "cz";
		}
		return state;

	}

	/**
	 * 调用后台dao，加载数据page，我的工作计划
	 * 
	 * @auther jack.zhao
	 * @param page
	 * @param criteria
	 * 
	 */
	@DataProvider
	public void myWorkPlan(Page<WorkPlan> page, Criteria criteria) {
		workPlanDao.myWorkPlanPage(page, criteria);
	}

	/**
	 * 所有state不是0的计划，
	 * 
	 * @auther jack.zhao
	 * @param page
	 * @param criteria
	 * 
	 */
	@DataProvider
	public void WorkPlanByState(Page<WorkPlan> page, Criteria criteria) {
		workPlanDao.searchAll(page, criteria);
	}

	/**
	 * 
	 */
	@DataProvider
	public void WorkPlanByStateOver(Page<WorkPlan> page, Criteria criteria) {
		workPlanDao.searchAllOver(page, criteria);
	}

	// //------------------------------------------------------------------------分割线

	/**
	 * 根据实体类的状态更新数据调用后台dao操作数据,普通更新
	 * 
	 * @auther jack.zhao
	 * @param entity
	 * 
	 */
	@DataResolver
	@Transactional
	public void saveAndUpdate(WorkPlan entity) {
		// final String state = "0";
		workPlanDao.updateWorkPlan(entity);
	}

	@DataResolver
	@Transactional
	public void saveAndTj(WorkPlan entity) {
		this.saveAndUpdate(entity);
		List<String> ids = new ArrayList<String>();
		ids.add(entity.getId());
		this.tj(ids);
	}

	/**
	 * 根据前台传来的ids进行删除数据
	 * 
	 * @auther jack.zhao
	 * @param ids
	 * 
	 */
	@Expose
	@Transactional
	public void deleteWorkPlan(List<String> ids) {
		for (String id : ids) {
			workPlanDao.deleteWorkPlanById(id);
		}

	}

	/**
	 * 得到本部门的科长下拉集合
	 * 
	 * @auther jack.zhao
	 * @return
	 * 
	 */
	@DataProvider
	@Transactional
	public List<IUser> getKezhang() {
		String deptId = ContextHolder.getLoginUser().getDepts().get(0).getId();

		// SecurityUtils.getUserByPositioinForZZ(deptId, "科长");
		List<IUser> users = SecurityUtils.getUserByRoleForDept(deptId, Constants.ROLE_KZ);
		return users;
	}

	/**
	 * 根据多个id进行数据的提交
	 * 
	 * @auther jack.zhao
	 * @param ids
	 * 
	 */
	@Expose
	@Transactional
	public void tj(List<String> ids) {
		for (String id : ids) {
			workPlanDao.tjWorkPlan(id);
		}
	}

	/**
	 * 
	 * 得到具计划负责人的用户
	 * 
	 * @auther jack.zhao
	 * 
	 */
	@DataProvider
	@Transactional
	public List<IUser> getJihuafuzeren() {
		String deptId = ContextHolder.getLoginUser().getDepts().get(0).getId();
		List<IUser> users = this.workPlanDao.getUserByDept(deptId);
		return users;
	}

	/**
	 * 根据parameter进行提交数据
	 * 
	 * @auther jack.zhao
	 * @param parameter
	 * 
	 */
	@Expose
	@Transactional
	public void tjfzr(Map<String, Object> parameter) {
		workPlanDao.tjfzr(parameter);
	}

	/**
	 * 最新版的提交负责人
	 *
	 *@auther jack.zhao 
	 *@param entitys
	 *@param ids
	 *
	 */
	@DataResolver
	@Transactional
	public void tjfzeEntitys(List<WorkPlan> entitys, List<String> ids) {
		for (WorkPlan w : entitys) {
			for (String id : ids) {
				if(w.getId().equals(id)){
					this.workPlanDao.update(w);
				}
			}

		}

	}

	/**
	 * 提交数据到处长
	 * 
	 * @auther jack.zhao
	 * @param ids
	 * 
	 */

	@Expose
	@Transactional
	public void tjchuzhang(Map<String, Object> parameter) {
		@SuppressWarnings("unchecked")
		List<String> ids = (List<String>) parameter.get("ids");
		String opinion = (String) parameter.get("opinion");
		for (String id : ids) {
			workPlanDao.tjChuzhangWorkPlan(id);
			final Opinion o = new Opinion();
			o.setBusKey(id);
			o.setBusTable("T_WORKPLAN");
			o.setOpinion(opinion);
			o.setProcessName("工作计划流程");
			o.setDeptId(ContextHolder.getLoginUser().getDepts().get(0).getId());
			o.setDeptName(ContextHolder.getLoginUser().getDepts().get(0).getName());
			o.setUsername(ContextHolder.getLoginUserName());
			o.setCname(ContextHolder.getLoginUser().getCname());
			// 保存科长的同意意见
			this.coreHibernateDao.save(o);
		}
	}

	/**
	 * 更改数状态，并且保存意见
	 * 
	 * @auther jack.zhao
	 * @param ids
	 * @param opinion
	 * @param img
	 * @param radiobutton
	 * 
	 */
	@Expose
	@Transactional
	public void ajaxKezhangBack(List<String> ids, String opinion, String img, int radiobutton) {
		if (ids.size() > 0) {
			for (String id : ids) {
				final Opinion o = new Opinion();
				o.setBusKey(id);
				o.setBusTable("T_WORKPLAN");
				if (radiobutton == 0) {
					o.setOpinion(opinion);
				} else if (radiobutton == 1) {
					if (StringUtils.isNotEmpty(img)) {
						o.setOpinionImg(img.getBytes());
					}
				}
				o.setProcessName("工作计划流程");
				o.setDeptId(ContextHolder.getLoginUser().getDepts().get(0).getId());
				o.setDeptName(ContextHolder.getLoginUser().getDepts().get(0).getName());
				o.setUsername(ContextHolder.getLoginUserName());
				o.setCname(ContextHolder.getLoginUser().getCname());
				// this.workPlanDao.save(o);
				this.coreHibernateDao.save(o);
				// 科长驳回
				this.workPlanDao.kezhangBack(id);
			}
		}
	}

	// * * 　　 へ　　　　　／|
	// 　　/＼7　　　 ∠＿/
	// 　 /　│　　 ／　／ 处长的ajax方法来了，列队欢迎！
	// 　│　Z ＿,＜　／　　 /`ヽ
	// 　│　　　　　ヽ　　 /　　〉
	// 　 Y　　　　　`　 /　　/
	// 　ｲ●　､　●　　⊂⊃〈　　/
	// 　()　 へ　　　　|　＼〈
	// 　　>ｰ ､_　 ィ　 │ ／／
	// 　 / へ　　 /　ﾉ＜| ＼＼
	// 　 ヽ_ﾉ　　(_／　 │／／
	// 　　7　　　　　　　|／
	// 　　＞―r￣￣`ｰ―＿

	/**
	 * 处长驳回
	 * 
	 * @auther jack.zhao
	 * @param ids
	 * @param opinion
	 * @param img
	 * @param radiobutton
	 * 
	 */
	@Expose
	@Transactional
	public void ajaxChuzhangBack(List<String> ids, String opinion, String img, int radiobutton) {
		if (ids.size() > 0) {
			for (String id : ids) {
				final Opinion o = new Opinion();
				o.setBusKey(id);
				o.setBusTable("T_WORKPLAN");
				if (radiobutton == 0) {
					o.setOpinion(opinion);
				} else if (radiobutton == 1) {
					if (StringUtils.isNotEmpty(img)) {
						o.setOpinionImg(img.getBytes());
					}
				}
				o.setProcessName("工作计划流程");
				o.setDeptId(ContextHolder.getLoginUser().getDepts().get(0).getId());
				o.setDeptName(ContextHolder.getLoginUser().getDepts().get(0).getName());
				o.setUsername(ContextHolder.getLoginUserName());
				o.setCname(ContextHolder.getLoginUser().getCname());
				this.coreHibernateDao.save(o);
				// 处长驳回
				this.workPlanDao.chuzhangBack(id);
			}
		}
	}

	/**
	 * 处长同意
	 * 
	 * @auther jack.zhao
	 * @param ids
	 * @param opinion
	 * @param img
	 * @param radiobutton
	 * 
	 */
	@Expose
	@Transactional
	public void ajaxChuzhangOk(List<String> ids, String opinion, String img, int radiobutton) {
		if (ids.size() > 0) {
			for (String id : ids) {
				final Opinion o = new Opinion();
				o.setBusKey(id);
				o.setBusTable("T_WORKPLAN");
				if (radiobutton == 0) {
					o.setOpinion(opinion);
				} else if (radiobutton == 1) {
					if (StringUtils.isNotEmpty(img)) {
						o.setOpinionImg(img.getBytes());
					}
				}
				o.setProcessName("工作计划流程");
				o.setDeptId(ContextHolder.getLoginUser().getDepts().get(0).getId());
				o.setDeptName(ContextHolder.getLoginUser().getDepts().get(0).getName());
				o.setUsername(ContextHolder.getLoginUserName());
				o.setCname(ContextHolder.getLoginUser().getCname());
				this.coreHibernateDao.save(o);
				// 处长Ok
				this.workPlanDao.executePlan(id);
			}
		}
	}

	/**
	 * 本次是执行的更新操作，所以
	 * 
	 * @auther jack.zhao
	 * @param entity
	 * 
	 */
	@DataResolver
	@Transactional
	public void doUpdate(WorkPlan entity) {
		this.workPlanDao.update(entity);
	}

	/**
	 * 
	 * 根据id把sate设置成60
	 * 
	 * @auther jack.zhao
	 * @param id
	 * 
	 */
	@Expose
	@Transactional
	public void doOver(List<String> ids) {
		for (String id : ids) {
			WorkPlan workPlan = this.workPlanDao.getWorkPlanById(id);
			workPlan.setState("60");
			workPlan.setWorkState("完成");
			this.workPlanDao.update(workPlan);
		}
	}

	/**
	 * 我的计划里面，保存并且执行
	 * 
	 * @auther jack.zhao
	 * @param entity
	 * 
	 */
	@DataResolver
	@Transactional
	public void doUpdateOver(WorkPlan entity) {
		this.workPlanDao.update(entity);
		List<String> ids = new ArrayList<String>();
		ids.add(entity.getId());
		this.doOver(ids);
	}

}
